/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.startup;

import ca.trader.beans.enterprise.TraderPersistenceConsts;
import ca.trader.logger.TraderLogger;
import ca.trader.regions.RegionManager;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import javax.servlet.ServletContext;

/**
 * This class manages the servlet context, which is initiated at the startup of
 * the server. The objects it currently manages is the RegionManager. At the
 * startup of the server, the RegionManager is automatically initiated, and cab
 * be extracted via the getRegionManager method
 *
 * @author Ahsan
 */
public class ServletContextManager
{

	 private static final String _className = ServletContextManager.class.getName();

	 /**
	  * Path to the RegionData.xml file. Initiated when the server starts in
	  * the startupInit method
	  */
	 private static String REGION_DATA_XML_PATH;

	 /**
	  * Attribute name that binds to the RegionManager in the SErvletContext
	  */
	 private static final String SERVLET_CONTEXT_REGION_MANAGER_ATTRIBUTE_NAME = "RegionManagerAttribute";

	 /**
	  * Reference to the ServletContext. Initiated when the server starts in
	  * the startupInit method
	  */
	 private static ServletContext SERVLET_CONTEXT;

	 /**
	  * This method is automatically called by StartupListener when the server
	  * starts up
	  *
	  * @param servletContext the servlet context object
	  */
	 @PersistenceContext(unitName = TraderPersistenceConsts.PERSISTENCE_UNIT_NAME, type = PersistenceContextType.EXTENDED)
	 private EntityManager entityManager;

	 public void startupInit(ServletContext servletContext)
	 {
		  String _methodName = "startupInit";

		  TraderLogger.logMethodEntry(_className, _methodName);

		  TraderLogger.logInfo(_className, _methodName, "Starting up initiation of servlet context manager");

		  // Get the path to the RegionData.xml
		  REGION_DATA_XML_PATH = servletContext.getRealPath("WEB-INF/classes/prod/RegionData.xml");

		  // Set the Servlet context object
		  SERVLET_CONTEXT = servletContext;
		  try
		  {
			   // Initiate the RegionManager object, bind it SERVLET_CONTEXT_REGION_MANAGER_ATTRIBUTE_NAME
			   RegionManager regionManager = RegionManager.fromXMLFile(REGION_DATA_XML_PATH);
			   servletContext.setAttribute(SERVLET_CONTEXT_REGION_MANAGER_ATTRIBUTE_NAME, regionManager);
		  }
		  catch (Exception ex)
		  {
			   TraderLogger.logSevere(_className, _methodName, _className, ex);
		  }

		  //Query q = entityManager.createNativeQuery("ALTER TABLE `sale` ADD FULLTEXT (TITLE, DESCRIPTION)");
		  //q.executeUpdate();
		  
		  TraderLogger.logMethodExit(_className, _methodName);
	 }

	 /**
	  * Returns the already-initialized getRegionManager object
	  *
	  * @return the region manager.
	  */
	 public static RegionManager getRegionManager()
	 {
		  return (RegionManager) SERVLET_CONTEXT.getAttribute(SERVLET_CONTEXT_REGION_MANAGER_ATTRIBUTE_NAME);
	 }

}
